//
//  ServiceViewController.m
//  JewelryStore
//
//  Created by apple on 2016/10/31.
//  Copyright © 2016年 com. All rights reserved.
//

#import "ServiceViewController.h"
#import "ServiceHeadCollectionView.h"
#import "ServiceCollectionViewCell.h"
#import "ServiceModel.h"

#define ServiceHeadView @"serviceHaedView"
#define ServiceCell @"serviceCell"

@interface ServiceViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *serviceCollectView;

@property (nonatomic, copy) NSArray *serviceCategoryData;
@property (nonatomic, strong) ServiceModel *model;
@end

@implementation ServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initUI];
}

#pragma mark - initUI

- (void)initUI
{
    [self.view addSubview:self.serviceCollectView];
    self.automaticallyAdjustsScrollViewInsets = NO;

}

#pragma mark - initData

- (void)initData
{
    _serviceCategoryData = @[@{@"headImage":@"f_fw", @"title":@"商品服务"},
                             @{@"headImage":@"d_dingzhi", @"title":@"定制服务"},
                             @{@"headImage":@"z_zixun", @"title":@"咨询服务"},
                             @{@"headImage":@"p_peitao", @"title":@"配套服务"}];
    [ServiceModel setup];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"ServiceJson"ofType:@"json"];
    NSData *jdata = [[NSData alloc]initWithContentsOfFile:filePath];
    _model = [ServiceModel mj_objectWithKeyValues:jdata];
    TT_Log(@"%@", jdata);
    
}

#pragma mark - 懒加载

- (UICollectionView *)serviceCollectView
{
    if (!_serviceCollectView) {
        //布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        
        _serviceCollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-45-64) collectionViewLayout:layout];
        _serviceCollectView.dataSource = self;
        _serviceCollectView.delegate = self;
        _serviceCollectView.backgroundColor = kBackgroundGray;
        _serviceCollectView.showsVerticalScrollIndicator = NO;
        
        //注册cell
        [_serviceCollectView registerNib:[UINib nibWithNibName:@"ServiceCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:ServiceCell];
        
        //注册head
        [_serviceCollectView registerNib:[UINib nibWithNibName:@"ServiceHeadCollectionView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ServiceHeadView];
    }
    return _serviceCollectView;
}

#pragma mark - collectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[_model.serviceData[section] detailData] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _model.serviceData.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize one = CGSizeMake(SCREEN_WIDTH/3.02, 100);
    CGSize two = CGSizeMake(SCREEN_WIDTH/4.04, 80);
    return indexPath.section < 2 ? one :two;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH/9.5);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    ServiceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ServiceCell forIndexPath:indexPath];
    cell.model = [_model.serviceData[indexPath.section] detailData][indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
       ServiceHeadCollectionView *tHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ServiceHeadView forIndexPath:indexPath];
        tHeadView.model = _model.serviceData[indexPath.section];
        headView = tHeadView;
    }
    return headView;
}


@end
